package com.tarena.lbs.business.servcie.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreRepository storeRepository;
    public PageResult<StoreVO> pageList(StoreQuery query) {
        PageResult<StorePO> result = storeRepository.queryStore(query);
        List<StorePO> list = result.getObjects();
        Long total = result.getTotal();
        List<StoreVO> vo = list.stream().map(this::convertToVO).collect(Collectors.toList());
        PageResult<StoreVO> pageVO = new PageResult<>();
        pageVO.setTotal(total);
        pageVO.setObjects(vo);
        pageVO.setPageNo(query.getPageNo());
        pageVO.setPageSize(query.getPageSize());
        return pageVO;
    }

    private StoreVO convertToVO(StorePO po) {
        StoreVO vo = new StoreVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    public void save(StoreParam param) {
        StorePO po = new StorePO();
        BeanUtils.copyProperties(param, po);
        po.setCreateTime(new Date());
        po.setStoreLongitude(param.getLongitude());
        po.setStoreLatitude(param.getLatitude());
        String ids = String.join(",",param.getStoreImagesIds());
        po.setStoreImagesId(ids);
        storeRepository.save(po);
    }

    public PageResult<StoreVO> getStoreByCity(AreaStoreQuery query) {
        return null;
    }
}
